<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 121 >>> true     123 >>> false
        /**
         * 1234321  123  123
         * 123321 
        */
        function getPalindromeNumber1 (num) {
            if (num < 0) {
                return false;
            }
            const numStr = String(num); // '1234321'
            const len = Math.floor(numStr.length / 2);
            let s1 = ''; // 从前往后
            let s2 = ''; // 从后往前
            for (let i = 0; i < len; i++) {
                s1 += numStr[i];
                s2 += numStr[numStr.length - 1 - i];
            }
            return s1 === s2;
        }
        
        // console.log(getPalindromeNumber(1234321)); // true
        // console.log(getPalindromeNumber(123321)); // true
        // console.log(getPalindromeNumber(123124141)); // false

        /**
         * 数学方式
         * 从后往前造一个数字  和原数字比较
         * 1234 >>> 123, 4 >>>  12, 3  >>> 1, 2   4321 === 1234
         * -1234
        */
        function getPalindromeNumber2 (num) {
            if (num < 0) {
                return false;
            }
            let newNum = 0; // 4
            const originNum = num;
            
            while (num > 0) {
                const unit = num % 10; // 3
                num = (num - unit) / 10; // 12
                newNum = newNum * 10 + unit;
            }

            return newNum === originNum;
        }

        console.log(getPalindromeNumber2(1234321)); // true
        console.log(getPalindromeNumber2(123321)); // true
        console.log(getPalindromeNumber2(123124141)); // false

    </script>
</body>
</html>